<!DOCTYPE html>
<html>
<head>
    <title>ads form</title>
    <#include "/header.html">
    <style>
        .layui-upload-img {
            max-height: 100px;
        }
    </style>
</head>
<body class="bg-white">

<div id="appMain">
    <form class="layui-form layuimini-form" action="" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" placeholder="输入名称" value="" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <input type="text" name="remark" placeholder="输入描述" value="" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否主导航</label>
            <div class="layui-input-block">
                <input type="radio" name="isMain" value="0" title="否">
                <input type="radio" name="isMain" value="1" title="是" checked>
            </div>
        </div>

        <div class="layui-form-item layui-hide">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal" id="commitBtn" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    layui.use(['form', 'laytpl', 'upload'], function () {
        var form = layui.form,
            laytpl = layui.laytpl,
            upload = layui.upload,
            $ = layui.$;

        var _parent = parent;
        /* laytpl(_parent.layui.jquery("#positionListTpl").html()).render(_parent.positionList, function(html){
            $("#positionListBox").html(html);
            form.render("select");
            if(_parent.formDataId > 0) {
                $.ajax({
                    type: "POST",
                    url: serverUrl('ads/info/' + _parent.formDataId),
                    success: function (result) {
                        if (result.code === 0) {
                            form.val('dataForm', result.info);
                            $('#uploadPreview').attr('src', result.info.image); //url
                        }
                    }
                });
            }
        });
 */
		//获取类型select
		 $.ajax({
				url:serverUrl("ads/positions"),
				type:"post",
				dataType:"",
				success:function(btn){
			          var select = $("#position");
				      	for (var j = 0; j < btn.count; j++) {
					      	  select.append("<option value='"+btn.data[j].position+"'>"+btn.data[j].position+"</option>");
				  		}
			          layui.form.render("select");
				},
				error:function(){
					console.log("获取失败");
				}
			})   
		 $(document).ready(function () {
		     if (parent.formDataId > 0) {
		         $.ajax({
		             type: "POST",
		             url: serverUrl('hcnav/info/' + parent.formDataId),
		             data: {},
		             dataType: "json",
		             success: function (result) {
		                 if (result.code === 0) {
		                     form.val('dataForm', result.hcNav);
		                        /* if (result.info.img != '') {
	                                $('#uploadPreview').attr('src', result.info.img); //url
		                        } */
		                 }
		             }
		         });
		     }
		 });
        var submitForm = function () {
            var params = form.val('dataForm');
            params['id'] = _parent.formDataId || null;

            var method = _parent.formDataId > 0 ? 'update' : 'save';
            submitFormData('hcnav/'+method, params);
        };

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#uploadBtn'
            ,url: serverUrl('oss/file/upload')
            ,auto: false
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#uploadPreview').attr('src', result); //base64
                });
            }
            ,done: function(res){
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                form.val('dataForm', {'img': res.url});
                submitForm();
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            submitForm();
            return false;
        });

    });
</script>
</body>
</html>